include config.mk

CUR_PATH=$(shell /bin/pwd)

PRJ_SRCS=\
src/gutil.c \
src/gconn.c \
src/gbase.c \
src/gmpi.c

PRJ_OBJS=$(PRJ_SRCS:.c=.o)

SRCS=$(PRJ_SRCS)
OBJS=$(PRJ_OBJS)

LIBGIGI=$(PRJ_LIB_ROOT)/libgigi.a

build: gigi gigitest

gtest:
	(cd ./tool && sh ./configure --prefix=$(CUR_PATH)/ )
	
	(cd ./sdk/gtest && \
	sh ./configure --prefix=$(CUR_PATH)/ && \
	make &&\
	make install &&\
	rm -rf $(CUR_PATH)/lib/libgtest*.so* )
	
	( cd ./test/gtestso && make build )

$(LIBGIGI): $(OBJS)
	mkdir -p $(PRJ_LIB_ROOT)
	ar -rs $@ $(OBJS)

$(OBJS): %.o:%.c
	$(CC) $(DBG) $(CCFLAGS) $(LCOVFLAGS) -c $< -o $@ $(INCLUDES) -fPIC -DPIC

gigi: $(LIBGIGI)

clean:
	rm -f $(LIBGIGI)
	rm -f $(OBJS)
	cd test/gigitest && make clean	
	(cd src && \
	for pattern in *.info *.gcda *.da *.gcno;\
	do find . -name $$pattern | xargs rm -f ;\
	done )

gigitest:
	cd test/gigitest && make clean build install

unittest:
	mkdir -p $(PRJ_ROOT)/log
	$(PRJ_BIN_PATH)/gigitest --gtest_output=xml:$(PRJ_ROOT)/log/test_gigitest.xml

testreport:
	for testcase in `echo $(CUR_PATH)/log/test_*.xml`;\
		do \
			perl -pi -e 's/^<testsuite/<testsuites/g' $$testcase;\
			perl -pi -e 's/^<\/testsuite>/<\/testsuites>/cg' $$testcase; \
			perl -pi -e 's/<testsuite name="/<testsuite name="test\./cg' $$testcase; \
			perl -pi -e 's/classname="/classname="test\./cg' $$testcase; \
		done

valgrind:
	mkdir -p log
	rm -f ./log/gigitest.valgrind.log.*
	valgrind --leak-check=full --log-file=./log/gigitest.valgrind.log ./bin/gigitest

lcov:
	rm -f $(CUR_PATH)/log/GIGI.CoverageResult.info
	lcov -q -d src --no-recursion -b . -c -t GIGI -o gigi.info --ignore-errors source	
	lcov -q --remove gigi.info  '*/include/*' '*ut_*' -o CovResultNEW.info
	cp -f CovResultNEW.info $(CUR_PATH)/log/GIGI.CoverageResult.info
	@rm *.info

install:
	mkdir -p $(INSTALL_ROOT)
	cp src/gigione.h $(INSTALL_ROOT)
	cp lib/libgigi.a $(INSTALL_ROOT)
